﻿using System.Configuration;

namespace SPDeployer.Core.Config
{
    public class WebConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty(Definitions.Webs.NAME, IsKey = false, IsRequired = false)]
        public string Name
        {
            get { return this[Definitions.Webs.NAME].ToString(); }
            set { this[Definitions.Webs.NAME] = value; }
        }

        [ConfigurationProperty(Definitions.Webs.URL, IsRequired = true)]
        public string Url
        {
            get { return this[Definitions.Webs.URL].ToString(); }
            set { this[Definitions.Webs.URL] = value; }
        }

        [ConfigurationProperty(Definitions.FEATURES_COLLECTION, IsRequired = false)]
        public FeatureConfigurationElementCollection Features
        {
            get { return (FeatureConfigurationElementCollection)this[Definitions.FEATURES_COLLECTION]; }
            set { this[Definitions.FEATURES_COLLECTION] = value; }
        }

        [ConfigurationProperty(Definitions.WEBS_COLLECTION, IsRequired = false)]
        public WebConfigurationElementCollection Webs
        {
            get { return (WebConfigurationElementCollection)this[Definitions.WEBS_COLLECTION]; }
            set { this[Definitions.WEBS_COLLECTION] = value; }
        }

        [ConfigurationProperty(Definitions.LISTS_COLLECTION, IsRequired = false)]
        public ListConfigurationElementCollection Lists
        {
            get { return (ListConfigurationElementCollection)this[Definitions.LISTS_COLLECTION]; }
            set { this[Definitions.LISTS_COLLECTION] = value; }
        }
    }

    [ConfigurationCollection(typeof(WebConfigurationElement), AddItemName = "Web")]
    public class WebConfigurationElementCollection : ConfigurationElementCollection
    {
        public WebConfigurationElement this[int index]
        {
            get { return BaseGet(index) as WebConfigurationElement; }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new WebConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((WebConfigurationElement)element).Url;
        }
    }
}